package com.HQCourse.vod.vod.controller;



import com.HQCourse.Exception.HQCourseException;
import com.HQCourse.result.Result;
import com.HQCourse.vod.vod.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.HQCourse.model.vod.Teacher;
import com.example.HQCourse.vo.vod.TeacherQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zww
 * @since 2023-06-17
 * 1318497587
 * SecretId: AKID4m6csSrXsT4TkKsLmFuDV7VuDOPc91L4
 *
 * SecretKey: wndqzW0Ldmrqhoscb4ShkcER1ObvLG6W
 */

@Api("teacher接口")
@RestController
@RequestMapping("admin/vod/teacher")
//@CrossOrigin
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
 /**   @GetMapping("/selectAll")
    @ApiOperation("查询所有")
    public List<Teacher> findAllTeacher(){
        List<Teacher> list = teacherService.list();
        return  list;
    }
  */
    @GetMapping("/selectAll")
    @ApiOperation("查询所有")
    public Result findAllTeacher(){
        List<Teacher> list = teacherService.list();
        return  Result.ok(list);
    }
    @DeleteMapping("/remove/{id}")
    @ApiOperation("根据id删除")
    public Result removeById(@ApiParam(name="id",value = "ID",required = true)
                                 @PathVariable("id")int id){
        boolean isSuccess = teacherService.removeById(id);
        if(isSuccess){
            return Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }
    //teacherQueryVo条件部分
    //@RequestBody(required = false) 接受传递过来的json数据 为false时表示可以为空，必须用post提交
    @ApiOperation("条件查询分页")
    @PostMapping("findQueryPage/{current}/{limit}")
    public Result findPage(@PathVariable("current") int current,
                           @PathVariable("limit")int limit,
                           @RequestBody(required = false) TeacherQueryVo teacherQueryVo){
        IPage<Teacher> iPage = teacherService.iPage(current, limit, teacherQueryVo);
        if(teacherQueryVo==null) return Result.fail(iPage);
        return Result.ok(iPage);
    }
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public Result addTeacher(@RequestBody(required = false) Teacher teacher){
         boolean save = teacherService.save(teacher);
         return save==true?Result.ok(null):Result.fail(null);
    }
    @ApiOperation("根据id查询")
    @GetMapping("teacherId/{id}")
    public Result teacher(@PathVariable("id") Long id){
        return Result.ok(teacherService.getById(id));
    }
    @ApiOperation("修改讲师")
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody(required = false) Teacher teacher){
        boolean flag = teacherService.updateById(teacher);
        return flag==true?Result.ok(null):Result.fail(null);
    }
    @ApiOperation("批量删除讲师")
    @DeleteMapping("removeBatch")
    public Result removeBatch(@RequestBody List<Long> idList){
        return teacherService.removeByIds(idList)==true?Result.ok(null):Result.fail(null).message("数据异常");
    }
    @ApiOperation("根据id查询")
    @GetMapping("getTeacherId/{id}")
    public Teacher getTeacherteacher(@PathVariable("id") Long id){
        return teacherService.getById(id);
    }

}

